package com.smile.config;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

@Configuration
public class GatewayConfig {

    // CORS 配置
//    @Bean
//    public CorsWebFilter corsWebFilter() {
//        CorsConfiguration config = new CorsConfiguration();
////        config.setAllowedOrigins(Arrays.asList("http://localhost:5173","http://localhost",
////                "http://127.0.0.1","http://192.168.52.1:8500")); // 设置允许的前端地址
//
////        config.setAllowedOrigins(Arrays.asList("http://localhost", "http://192.168.52.1:8500",
////                "http://localhost:10086",
////                "http://localhost:5173"));
//        config.setAllowedOrigins(Arrays.asList("*"));
//        config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
//        config.setAllowedHeaders(Arrays.asList("*"));
////        config.setAllowCredentials(true);
//        config.addExposedHeader("Location");
//        config.setMaxAge(3600L);
//
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", config); // 应用到所有路由路径
//
//        return new CorsWebFilter(source);
//    }


//    @Bean
//    public CorsWebFilter corsWebFilter() {
//        CorsConfiguration config = new CorsConfiguration();
//        config.setAllowedOrigins(Arrays.asList("*")); // 允许所有来源（生产环境建议明确指定）
//        config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
//        config.setAllowedHeaders(Arrays.asList("*"));
//        config.addExposedHeader("Location");
//        config.setMaxAge(3600L);
//
//        // 创建排除特定路径的配置源
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/api/user/**", config); // 默认空配置
////        source.registerCorsConfiguration("/**", config); // 其他路径应用您的配置
//
//        return new CorsWebFilter(source);
//    }

    // 路由配置

    //          filters:
    //#            - RewritePath=/v3/api-docs/(.*), /smile-recruit-admin/v3/api-docs/$1
    //            - AddResponseHeader=Access-Control-Allow-Origin, "*"
    //            - AddResponseHeader=Access-Control-Allow-Methods, "GET, POST, PUT, DELETE, OPTIONS"
    //            - AddResponseHeader=Access-Control-Allow-Headers, "*"
    //        - id: smile-recruit-oss-doc
    //          uri: lb://smile-recruit-oss
    //          order: 0
    //          predicates:
    //            - Path=/v3/api-docs/**
    //          filters:
    //#            - RewritePath=/v3/api-docs/(.*), /smile-recruit-oss/v3/api-docs/$1
    //            - AddResponseHeader=Access-Control-Allow-Origin, "*"
    //            - AddResponseHeader=Access-Control-Allow-Methods, "GET, POST, PUT, DELETE, OPTIONS"
    //            - AddResponseHeader=Access-Control-Allow-Headers, "*"

//    @Bean
//    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
//        return builder.routes()
//                .route("qq_route", r -> r.path("/red").uri("https://www.baidu.com"))
//                .route("smile-plus-standalone-user-user", r -> r.path("/user/**")
////                        .filters(f -> f.rewritePath("/smile-recruit-oss/v3/api-docs", "/v3/api-docs"))
//                        .uri("http://localhost:8500"))
//
//                .route("smile-plus-standalone-user-role", r -> r.path("/role/**")
////                        .filters(f -> f.rewritePath("/smile-recruit-oss/v3/api-docs", "/v3/api-docs"))
//                        .uri("http://localhost:8500"))
//
//                .route("smile-plus-standalone-user-menu", r -> r.path("/menu/**")
////                        .filters(f -> f.rewritePath("/smile-recruit-oss/v3/api-docs", "/v3/api-docs"))
//                        .uri("http://localhost:8500"))
//
//                .route("smile-plus-standalone-sso", r -> r.path("/sso/**")
////                        .filters(f -> f.rewritePath("/smile-recruit-oss/v3/api-docs", "/v3/api-docs"))
//                        .uri("http://localhost:8600"))
//
//
//
//                // 目标服务地址
//                .build();
//    }

//    @Bean
//    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
//        return builder.routes()
//                .route("smile-recruit-admin-doc", r -> r.path("/v3/api-docs/**")
////                        .filters(f->{
////                    return
////                    f.rewritePath("/v3/api-docs/(?<remaining>.*)", "/smile-recruit-admin/v3/api-docs/${remaining}");
////                })
//                        .uri("http://192.168.52.1:8700"))
//                .route("smile-recruit-admin-webjars", r -> r.path("/webjars/**")
//                        .uri("http://192.168.52.1:8700"))
//                .route("smile-recruit-oss-doc",
//                        r-> r.path("/v3/api-docs/**")
////                                .filters(f->{
////                                    return
////                                            f.rewritePath("/v3/api-docs/(?<remaining>.*)", "/smile-recruit-oss/v3/api-docs/${remaining}");
////                                })
//                                .
//                                uri("http://192.168.52.1:8800"))
//                .route("smile-recruit-admin-idCard", r -> r.path("/idCard/**")
//                        .uri("http://192.168.52.1:8700"))
//                .route("smile-security-auth-login", r -> r.path("/sso/**")
//                        .uri("http://192.168.52.1:8500"))
//                .route("smile-recruit-oss",r-> r.path("/*").uri("http://192.168.52.1:8800"))
//
//                // 目标服务地址
//                .build();
//    }
}


